//
//  MyUserAccount.swift
//  weibo08
//
//  Created by 陈杰明 on 16/2/29.
//  Copyright © 2016年 JM. All rights reserved.
//

import UIKit

class MyUserAccount: NSObject, NSCoding {

    //用于调用access_token，接口获取授权后的access_token
    var access_token: String?
    //当前授权的用户的Uid
    var uid: String?
    //access_token 的生命周期
    //在swift下数据类型定义为可选，在kvc下会找不到
    var expires_in: NSTimeInterval = 0 {
        didSet{
            expriesDate = NSDate(timeIntervalSinceNow: expires_in)
        }
    }
    
    //
    var expriesDate: NSDate?
    
    //用户名称
    var screen_name: String?
    //头像
    var avatar_large: String?
    
    //一键转模型
    init(dict: [String: AnyObject]) {
        
        super.init()
        //kvc
        self.setValuesForKeysWithDictionary(dict)
    }
    //忽略没有定义的key
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        print("forUndefinedKey:\(key)")
    }
    //归档
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeDouble(expires_in, forKey: "expires_in")
        aCoder.encodeObject(expriesDate, forKey: "expriesDate")
        aCoder.encodeObject(screen_name, forKey: "screen_name")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
    }
    //解档
    required init?(coder aDecoder: NSCoder) {
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        uid = aDecoder.decodeObjectForKey("uid") as? String
        expires_in = aDecoder.decodeDoubleForKey("expires_in")
        expriesDate = aDecoder.decodeObjectForKey("expriesDate") as? NSDate
        screen_name = aDecoder.decodeObjectForKey("screen_name") as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
    }
    //只读计算性属性
    override var description: String {
        
            let key = ["access_token", "uid", "expires_in", "expriesDate", "screen_name", "avatar_large"]
        //key是模型中的属性，通过key去模型中获取对应属性的值，然后组成字典
            return "userAccount模型:\(dictionaryWithValuesForKeys(key).description)"
        
    }
}
